$(function () {
    show_admin_name();
    init_company();
    edit_company();
    select_fund_msg();
});

function show_admin_name() {
    $.ajax({
        url: '/admin-info',
        type: 'GET',
        dataType: 'JSON',
        success: function (data) {
            if (data.status) {
                $('#show-admin-name').text(data.result);
            } else {
                console.log('wrong');
                alert('读取管理员信息失败')
            }
        }
    });
}

function init_company() {
    $.ajax({
        url: '/company-info',
        type: 'get',
        dataType: 'JSON',
        success: function (data) {
            if (data.status) {
                $('#show-company-name').text(data.result['name']);
                $('#show-company-owner').text(data.result['owner']);
                $('#show-company-listen').text(data.result['listen']);
            } else {
                alert('读取数据库失败');
            }
        }
    });
}

function edit_company() {
    var target_text = undefined;
    $('#edit-company').click(function () {
        target_text = $(this).parent().parent().children('div[class=inline-show]');
        $('#company-name').val(target_text.eq(0).children('h4').text());
        $('#company-owner').val(target_text.eq(1).children('h4').text());
        $('#company-id').val(target_text.eq(2).children('h4').text());
        $('#edit-Modal').modal('show');
    });
    $('#edit-confirm').click(() => {
        if($('#company-name').val() != '' && $('#company-owner').val() != '' && $('#company-id').val() != '') {
            $('#edit-confirm').attr('class', 'btn btn-success');
            $('#edit-confirm').text('正在添加..');
            $.ajax({
                url: '/edit-company-info',
                type: 'POST',
                dataType: 'JSON',
                data: $('#edit-form').serialize(),
                success: function (data) {
                    if (data.status) {
                        setTimeout(function () {
                            $('#edit-confirm').attr('class', 'btn btn-warning');
                            $('#edit-confirm').text('确认修改');
                            target_text.eq(0).children('h4').text($('#company-name').val());
                            target_text.eq(1).children('h4').text($('#company-owner').val());
                            target_text.eq(2).children('h4').text($('#company-id').val());
                            $('#edit-Modal').modal('hide');
                        }, 1500);
                    } else {
                        alert('上传数据失败');
                    }
                }
            });
        } else {
            $('#edit-confirm').attr('class', 'btn btn-danger');
            $('#edit-confirm').text('请完善信息后重试');
        }
    });
}

function select_fund_msg() {
    $('#fund-manager').click(() => {
        $.ajax({
            url: '/fund-info',
            type: 'get',
            dataType: 'json',
            success: function (data) {
                if (data.status) {
                    var result = new Array(data.result);
                    result.forEach((val, _idx, _arr) => {
                        var target_dic = JSON.parse(val);
                        $('#table-body').empty();
                        $('#all-fund').text('资金余额：' + target_dic['fund']);
                        target_dic['fund_detail'].forEach((sub_val, _sub_idx, _sub_arr) => {
                            console.log(sub_val);
                            $t_body = `
                                <tr>
                                    <td>${sub_val['id']}</td>
                                    <td>${sub_val['change_num']}</td>
                                    <td>${sub_val['change_cause']}</td>
                                </tr>
                            `;
                            $('#table-body').append($t_body);
                        });
                    });
                    $('#fund-Modal').modal('show');
                } else {
                    alert('数据读取失败');
                }
            }
        });
    });
}